﻿using EnvDTE;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using jTCodeGenerate.Models;

namespace jTCodeGenerate
{
    public partial class TreeViewProjectItemControl : UserControl
    {
        public TreeViewProjectItemControl()
        {
            InitializeComponent();
        }
        public void BandDataSource(List<ProjectModel> projectModels)
        {
            treeView1.Nodes.Clear();            
            foreach (var d in projectModels)
            {
                TreeNode node = new TreeNode();
                node.Name = d.Name;
                node.Text = d.Name;
                node.Tag = d.Project;
                LoadChildren(ref node, d.ProjectItemModels);
                treeView1.Nodes.Add(node);
            }
            treeView1.ExpandAll();
        }

        private void LoadChildren(ref TreeNode node, IList<ProjectItemModel> projectModels)
        {
            if (projectModels == null)
                return;
            foreach (var item in projectModels)
            {
                var n = new TreeNode { Name = item.Name, Text = item.Name, Tag = item.Item };
                node.Nodes.Add(n);
                if (item.Childs != null && item.Childs.Count > 0)
                    LoadChildren(ref n, item.Childs);
            }
        }

        public TreeNode SelectedNode
        {
            get
            {
                return treeView1.SelectedNode;
            }
        }

        private void addFolder_Click(object sender, EventArgs e)
        {
            var node = treeView1.SelectedNode;
            if (node != null)
            {
                RenameDialogForm renameDialog = new RenameDialogForm();
                renameDialog.BtnOkClick = (name) =>
                {
                    Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();
                    if (node.Tag is Project)
                    {
                        var project = node.Tag as Project;
                        if (project.ProjectItems.Item(name) == null)
                        {
                            project.ProjectItems.AddFolder(name);
                        }
                    }
                    if (node.Tag is ProjectItem)
                    {
                        var project = node.Tag as ProjectItem;
                        if (project.ProjectItems.Item(name) == null)
                        {
                            project.ProjectItems.AddFolder(name);
                        }
                    }
                    BandDataSource(MainCommandBase.Instance.InitProjectFolderItem());
                };
                renameDialog.ShowDialog();
            }
        }

        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Button != MouseButtons.Right) return;
            if (e.Node.Parent == null || e.Node == null) return;
            treeView1.SelectedNode = e.Node;
            contextMenuStrip1.Show(e.X, e.Y);
        }
    }
}
